package org.springframework.samples.countries.web.translate;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.commons.EmptyVisitor;

public class AnnotationCheckingTranslator extends EmptyVisitor {

	private EmptyVisitor delegate = null;

	private boolean isValid = false;

	public AnnotationCheckingTranslator(EmptyVisitor translator) {
		this.delegate = translator;
	}

	@Override
	public AnnotationVisitor visitAnnotation(String description, boolean visible) {
		if (description.contains("ClientSide")) {
			isValid = true;
			return new EmptyVisitor();
		}
		else {
			return null;
		}
	}

	@Override
	public AnnotationVisitor visitAnnotationDefault() {
		return new EmptyVisitor();
	}

	@Override
	public void visitCode() {
		if (isValid) {
			delegate.visitCode();
		}

	}

	@Override
	public void visitEnd() {
		if (isValid) {
			delegate.visitEnd();
		}

	}

	@Override
	public void visitFieldInsn(int opcode, String owner, String name, String desc) {
		if (isValid) {
			delegate.visitFieldInsn(opcode, owner, name, desc);
		}
	}

	@Override
	public void visitInsn(int opcode) {
		if (isValid) {
			delegate.visitInsn(opcode);
		}
	}

	@Override
	public void visitMethodInsn(int opcode, String owner, String name, String desc) {
		if (isValid) {
			delegate.visitMethodInsn(opcode, owner, name, desc);
		}
	}

	@Override
	public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
		if (isValid) {
			delegate.visitLocalVariable(name, desc, signature, start, end, index);
		}
	}

	@Override
	public void visitVarInsn(int opcode, int var) {
		if (isValid) {
			delegate.visitVarInsn(opcode, var);
		}

	}

}